home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Moscow ML 1.31 / source code / mosml / src / mosmllib / test / integer.sml < prev    next >
Encoding:
Text File  |  1996-07-03  |  4.3 KB  |  145 lines  |  [TEXT/R*ch]

  1. (* test/integer.sml -- here we test only the `exotic' operations
  2.    PS 1995-02-25 *)
  3.  
  4. use "auxil.sml";
  5.  
  6. local 
  7.     open Integer
  8.     fun divmod (i, d, q, r)  = check(i div d = q andalso i mod d = r);
  9.     fun quotrem (i, d, q, r) = check(i quot d = q andalso i rem d = r);
  10. in    
  11.  
  12. val test1a = divmod(10, 3, 3, 1);
  13. val test1b = divmod(~10, 3, ~4, 2);
  14. val test1c = divmod(~10, ~3, 3, ~1);
  15. val test1d = divmod(10, ~3, ~4, ~2);
  16.  
  17. val test2a = quotrem(10, 3, 3, 1);
  18. val test2b = quotrem(~10, 3, ~3, ~1);
  19. val test2c = quotrem(~10, ~3, 3, ~1);
  20. val test2d = quotrem(10, ~3, ~3, 1);
  21.  
  22. val test3 = check(max(~5, 2) =  2 andalso max(5, 2) = 5);
  23. val test4 = check(min(~5, 3) = ~5 andalso min(5, 2) = 2);
  24.  
  25. val test5 = check(sign ~57 = ~1 andalso sign 99 = 1 andalso sign 0 = 0);
  26. val test6 = check(sameSign(~255, ~256) andalso sameSign(255, 256) 
  27.           andalso sameSign(0, 0));
  28.  
  29. local 
  30.     val args = [0.0, 99.0, ~5.0, 1.1, ~1.1, 1.9, ~1.9, 2.5, ~2.5, 
  31.         1000001.4999, ~1000001.4999];
  32. in
  33. val test7  = check(map ceil args  
  34.            = [0, 99, ~5, 2, ~1, 2, ~1, 3, ~2, 1000002, ~1000001]);
  35. val test8  = check(map floor args 
  36.            = [0, 99, ~5, 1, ~2, 1, ~2, 2, ~3, 1000001, ~1000002]);
  37. val test9  = check(map trunc args 
  38.            = [0, 99, ~5, 1, ~1, 1, ~1, 2, ~2, 1000001, ~1000001]);
  39. val test10 = check(map round args 
  40.            = [0, 99, ~5, 1, ~1, 2, ~2, 2, ~2, 1000001, ~1000001]);
  41. end
  42.  
  43. val test11 = check(0.0 = real 0 andalso 2.0 = real 2 andalso ~2.0 = real ~2);
  44.  
  45. val test12 = 
  46.     case (minint, maxint) of
  47.     (SOME mi, SOME ma) =>
  48.         check(sign mi = ~1 andalso sign ma = 1 
  49.           andalso sameSign(mi, ~1) andalso sameSign(ma, 1))
  50.       | (NONE, NONE)       => "OK"
  51.       | _                  => "WRONG";
  52.  
  53. fun chk f (s, r) = 
  54.     check'(fn _ => 
  55.        case f s of
  56.            SOME res => res = r
  57.          | NONE     => false)
  58.  
  59. fun chkScan fmt = chk (StringCvt.scanString (scan fmt))
  60.  
  61. val test13a = 
  62.     List.map (chk fromString)
  63.              [("10789", 10789),
  64.           ("+10789", 10789),
  65.           ("~10789", ~10789),
  66.           ("-10789", ~10789),
  67.           (" \n\t10789crap", 10789),
  68.           (" \n\t+10789crap", 10789),
  69.           (" \n\t~10789crap", ~10789),
  70.           (" \n\t-10789crap", ~10789)];
  71.  
  72. val test13b = 
  73.     List.map (fn s => case fromString s of NONE => "OK" | _ => "WRONG")
  74.        ["", "-", "~", "+", " \n\t", " \n\t-", " \n\t~", " \n\t+", 
  75.         "+ 1", "~ 1", "- 1", "ff"];        
  76.  
  77. val test14a = 
  78.     List.map (chkScan StringCvt.DEC)
  79.              [("10789", 10789),
  80.           ("+10789", 10789),
  81.           ("~10789", ~10789),
  82.           ("-10789", ~10789),
  83.           (" \n\t10789crap", 10789),
  84.           (" \n\t+10789crap", 10789),
  85.           (" \n\t~10789crap", ~10789),
  86.           (" \n\t-10789crap", ~10789)];
  87.  
  88. val test14b = 
  89.     List.map (fn s => case StringCvt.scanString (scan StringCvt.DEC) s 
  90.                   of NONE => "OK" | _ => "WRONG")
  91.        ["", "-", "~", "+", " \n\t", " \n\t-", " \n\t~", " \n\t+", 
  92.         "+ 1", "~ 1", "- 1", "ff"];        
  93.  
  94. val test15a = 
  95.     List.map (chkScan StringCvt.BIN)
  96.              [("10010", 18),
  97.           ("+10010", 18),
  98.           ("~10010", ~18),
  99.           ("-10010", ~18),
  100.           (" \n\t10010crap", 18),
  101.           (" \n\t+10010crap", 18),
  102.           (" \n\t~10010crap", ~18),
  103.           (" \n\t-10010crap", ~18)];
  104.  
  105. val test15b = 
  106.     List.map (fn s => case StringCvt.scanString (scan StringCvt.BIN) s 
  107.                   of NONE => "OK" | _ => "WRONG")
  108.        ["", "-", "~", "+", " \n\t", " \n\t-", " \n\t~", " \n\t+", 
  109.         "+ 1", "~ 1", "- 1", "2", "8", "ff"];
  110.  
  111. val test16a = 
  112.     List.map (chkScan StringCvt.OCT)
  113.              [("2071", 1081),
  114.           ("+2071", 1081),
  115.           ("~2071", ~1081),
  116.           ("-2071", ~1081),
  117.           (" \n\t2071crap", 1081),
  118.           (" \n\t+2071crap", 1081),
  119.           (" \n\t~2071crap", ~1081),
  120.           (" \n\t-2071crap", ~1081)];
  121.  
  122. val test16b = 
  123.     List.map (fn s => case StringCvt.scanString (scan StringCvt.OCT) s 
  124.                   of NONE => "OK" | _ => "WRONG")
  125.        ["", "-", "~", "+", " \n\t", " \n\t-", " \n\t~", " \n\t+", 
  126.         "+ 1", "~ 1", "- 1", "8", "ff"];
  127.  
  128. val test17a = 
  129.     List.map (chkScan StringCvt.HEX)
  130.              [("20Af", 8367),
  131.           ("+20Af", 8367),
  132.           ("~20Af", ~8367),
  133.           ("-20Af", ~8367),
  134.           (" \n\t20AfGrap", 8367),
  135.           (" \n\t+20AfGrap", 8367),
  136.           (" \n\t~20AfGrap", ~8367),
  137.           (" \n\t-20AfGrap", ~8367)];
  138.  
  139. val test17b = 
  140.     List.map (fn s => case StringCvt.scanString (scan StringCvt.HEX) s 
  141.                   of NONE => "OK" | _ => "WRONG")
  142.        ["", "-", "~", "+", " \n\t", " \n\t-", " \n\t~", " \n\t+", 
  143.         "+ 1", "~ 1", "- 1"];
  144. end
  145.